<template>
  <div class="box">
    <h2>Son1 子组件</h2>
    从vuex中获取值：<span>{{ count }}</span>
    <br>
    从vuex中获取值：<span>{{ number }}</span>
    <br>
    <button @click="addCount(1)">值 + 1</button>
    <button @click="addCount(5)">值 + 5</button>
    <br>
    <button @click="addNumber(1)">值 + 1</button>
    <button @click="addNumber(5)">值 + 5</button>
    <button @click="addNumberAction(5)">2s后+5</button>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions } from 'vuex'

export default {
  name: 'Son1Com',
  computed: {
    // 以下2种方式都可以获取store中的值
    // count () {
    //   return this.$store.state.count
    // }
    ...mapState(['count']),

    // 分模块
    // number () {
    //   return this.$store.state.user.number
    // }
    ...mapState('user', ['number'])
  },
  methods: {
    // addCount (n) {
    //   this.$store.commit('addCount', n)
    // },
    ...mapMutations(['addCount']),

    // 分模块
    // addNumber (n) {
    //   this.$store.commit('user/addNumber', n)
    // }
    ...mapMutations('user', ['addNumber']),

    // addNumberAction (num) {
    //   this.$store.dispatch('user/addNumberAction', num)
    // }
    ...mapActions('user', ['addNumberAction'])
  }
}
</script>

<style lang="css" scoped>
.box {
  border: 3px solid #ccc;
  width: 400px;
  padding: 10px;
  margin: 20px;
}

h2 {
  margin-top: 10px;
}
</style>
